package com.itheima.sh.web;

import com.itheima.sh.pojo.User;
import com.itheima.sh.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.处理post请求乱码
         request.setCharacterEncoding("utf-8");
        //2.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //3.将请求参数封装到User实体类对象中
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        //4.创建service业务层对象
        UserServiceImpl userService = new UserServiceImpl();
        //5.使用业务层对象调用业务层登录方法将User实体类对象传递给业务层
        User u=  userService.login(user);
        //6.判断u是否等于null
        if(u!=null){
            //7.如果等于null，说明没有查询到用户，登录失败，响应给前端false
            response.getWriter().print(false);
        }else{
            //8.如果不等于null，说明查询到用户了，登录成功，响应给前端true
            response.getWriter().print(true);
        }
    }
}